package com.program.cloudmanagesystem.login.config;

import com.program.cloudmanagesystem.login.domain.Hotel;
import com.program.cloudmanagesystem.login.domain.State;
import com.program.cloudmanagesystem.login.persistence.HotelFeign;
import com.program.cloudmanagesystem.login.service.StateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
@EnableFeignClients(basePackages = "com.program.cloudmanagesystem.login.persistence")
@Component
public class Schedule {
    @Autowired
    private StateService stateService;
    @Autowired
    private HotelFeign hotelFeign;

    @Scheduled(fixedRate = 5000)
    public void test(){
        int a = 0;
        List<Hotel> hotelList = hotelFeign.send();
        while (a<hotelList.size()){
            String c = String.valueOf(a);
            Hotel hotel = hotelList.get(a);
            float parking = Float.valueOf(hotel.getParking())/Float.valueOf(hotel.getMaxparking());
            float check = Float.valueOf(hotel.getCheck())/Float.valueOf(hotel.getMaxcheck());
            float capacity = Float.valueOf(hotel.getCapacity())/Float.valueOf(hotel.getMaxcapacity());
            if(stateService.saerchState(hotel.getId()) == null){
                State state = new State();
                state.setObject(hotel.getId());
                state.setParkingrate(String.valueOf(parking));
                state.setCheckrate(String.valueOf(check));
                state.setCapacityrate(String.valueOf(capacity));
                stateService.addState(state);
            }
            if(stateService.saerchState(hotel.getId()) != null){
                State state = new State();
                state.setObject(hotel.getId());
                state.setParkingrate(String.valueOf(parking));
                state.setCheckrate(String.valueOf(check));
                state.setCapacityrate(String.valueOf(capacity));
                stateService.updateNow(state);
            }
            a++;
        }


    }
}
